<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs title="WaveDigest Registration Gadget">
    <Require feature="wave" /> 
    <Require feature="locked-domain" />
    <Require feature="dynamic-height" />
  </ModulePrefs>
  <Content type="html">
    <![CDATA[     
	    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
		<script type="text/javascript">
	
			google.load("jquery", "1.3.2");
			google.load("jqueryui", "1.7.2");
			
			var wid = '::wid::';

			function init() 
			{
				console.log('init()');
  				if (wave && wave.isInWaveContainer()) 
  				{
  					wave.setStateCallback(renderInfo);
					wave.setParticipantCallback(renderInfo);
				}
				
				getRegistrations();
			}
			
			function buttonClicked() 
			{
	  			var viewerId = wave.getViewer().getId();
				var state = wave.getState();
				var email = document.getElementById('email').value;
				var schedule = parseInt(document.getElementById('schedule').value);

				delta = {};
			    delta[viewerId + '-email'] = email;
			    delta[viewerId + '-schedule'] = schedule;
			    state.submitDelta(delta);
			}
			
			function getRegistrations()
			{
				console.log('getRegistrations()');
				var params = {};  
				params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;  
				var url = "http://wavedigest.appspot.com/registrations?wid=" + escape(wid);
				console.log('sending request to: ' + url);
				
  				var sep = "?";
  				if (url.indexOf("?") > -1) 
  				{
    				sep = "&";
  				}
  				url = [ url, sep, "nocache=", new Date().getTime() ].join("");
				gadgets.io.makeRequest(url, getRegistrationsResponse, params);
			}
			
			function getRegistrationsResponse(obj)
			{
				console.log('getRegistrationsResponse()');
				console.log(JSON.stringify(obj));
				document.getElementById('registrations').innerHTML = obj.text;
			}
			
			function renderInfo() 
			{
				if (!wave.getState()) 
				{
					return;
				}
				
				document.getElementById('email').value = "";
				document.getElementById('schedule').selectedIndex = 0;
				
				getRegistrations();
			}
			
			gadgets.util.registerOnLoadHandler(init);
		</script>

		<div>To be notified when this wave changes fill in your email address and choose a schedule.</div>
		<div>
			Email: <input id="email" size="40" value="">
			<select id="schedule">
				<option value="0">Whenever a change occurs</option>
				<option value="1">Hourly</option>
				<option value="2">Daily</option>
				<option value="3">Weekly</option>
				<option value="4">Unsubscribe</option>
			</select>
			<button id="submit" onclick="buttonClicked()">Submit</button><br/>
		</div>
		
		<div>Current Registrations:</div>
		<div id="registrations">
			refreshing...
		</div>
    ]]>
  </Content>
</Module>